package com.happypets.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.happypets.servicesImpl.PetServiceImpl;

/**
 * Servlet implementation class PetComms
 */
public class PetComms extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PetComms() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=UTF-8");
		response.setHeader("Cache-Control", "no-store"); 
		
		PetServiceImpl psi = new PetServiceImpl();
		
//		HttpSession session = request.getSession();
		
		PrintWriter out = response.getWriter();
		
		List<Map<String,String>> lm = null;
		
		String id = request.getParameter("id");
		String flag = request.getParameter("flag");
		
		if(flag.equals("1")){
			lm = psi.petComm(id);
		}else{
			lm = psi.petSaleHis(id);
		}
		
		
		
		
		Gson gson=new Gson();
		String json=gson.toJson(lm);
		System.out.println(json);
		out.write(json);
		
		out.flush();
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
